package com.danco.training.zaneuskaya.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.danco.training.zaneuskaya.communication.Message;
import com.danco.training.zaneuskaya.communication.MessageAnalyzer;
import com.danco.training.zaneuskaya.service.impl.BookServiceImpl;

public class ServerThread extends Thread {

	private ObjectOutputStream os;
	private ObjectInputStream is;
	private InetAddress addr;
	private static final Logger logger = Logger.getLogger(ServerThread.class);

	public ServerThread(Socket socket) {
		try {
			os = new ObjectOutputStream(socket.getOutputStream());
			is = new ObjectInputStream(socket.getInputStream());

			addr = socket.getInetAddress();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}

	}

	public void run() {
		System.out.println("server Thread");

		try {
			while (true) {
				Object object = MessageAnalyzer.analizeObject((Message) is
						.readObject());
				os.writeObject(object);
			}
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage(), e);
		} catch (SecurityException e) {
			logger.error(e.getMessage(), e);
		} catch (InstantiationException e) {
			logger.error(e.getMessage(), e);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage(), e);
		} catch (IllegalArgumentException e) {
			logger.error(e.getMessage(), e);
		} catch (InvocationTargetException e) {
			logger.error(e.getMessage(), e);
		}
	}

}
